package site.zhouinfo.servlet04;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhou
 * @create 2017-06-27 16:12
 * @email zhouinfo@qq.com
 * @blog blog.zhouinfo.site
 */
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String nameTransform = new String(name.getBytes("iso8859-1"), "utf-8");
        String password = req.getParameter("pass");

        System.out.println("从表单中获取到的name: " + name);
        System.out.println("经过处理的name: " + nameTransform);
        System.out.println("从表单中获取到的password: " + password);

        if ("zhou".equals(nameTransform) && "zhou".equals(password)) {
            req.setAttribute("login_info", "zhou");
            req.getRequestDispatcher("/index.jsp").forward(req,resp);

            // 请求重定向 可以跳转出到其他url中
            //resp.sendRedirect("/index.jsp");
        } else {
            req.setAttribute("error", "用户名与密码不正确");
            req.getRequestDispatcher("/view/servlet04/login.jsp").forward(req,resp);

            // 请求重定向 可以跳转出到其他url中
            //resp.sendRedirect("/view/servlet04/login.html");
            //resp.sendRedirect("https://www.baidu.com");
        }
    }
}
